<template>
  <a-card :bordered="false" class="card-area">
    <div :class="advanced ? 'search' : null">
      <!-- 搜索区域 -->
      <a-form layout="horizontal">
        <div :class="advanced ? null: 'fold'">
          <a-row>
            <a-col :md="12" :sm="24">
              <a-form-item label="小区" :labelCol="{span: 5}" :wrapperCol="{span: 18, offset: 1}">
                <a-input v-model="queryParams.commName" />
              </a-form-item>
            </a-col>
          </a-row>
        </div>
        <span style="float: right; margin-top: 3px;">
          <a-button type="primary" @click="search">查询</a-button>
          <a-button style="margin-left: 8px" @click="reset">重置</a-button>
        </span>
      </a-form>
    </div>
    <div>
      <div class="operator">
        <a-button v-hasPermission="'community:add'" ghost type="primary" @click="add">新增</a-button>
        <a-button v-hasPermission="'community:delete'" @click="batchDelete">删除</a-button>
        <a-dropdown v-hasPermission="'community:export'">
          <a-menu slot="overlay">
            <a-menu-item key="export-data" @click="exprotExccel">导出Excel</a-menu-item>
          </a-menu>
          <a-button>
            更多操作
            <a-icon type="down" />
          </a-button>
        </a-dropdown>
      </div>
      <!-- 表格区域 -->
      <a-table ref="TableInfo" :columns="columns" :dataSource="dataSource" :pagination="pagination" :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}" :scroll="{ x: 900 }"
        @change="handleTableChange">
        <template slot="remark" slot-scope="text, record">
          <a-popover placement="topLeft">
            <template slot="content">
              <div style="max-width: 200px">{{text}}</div>
            </template>
            <p style="width: 200px;margin-bottom: 0">{{text}}</p>
          </a-popover>
        </template>
        <template slot="operation" slot-scope="text, record">
          <a-icon v-hasPermission="'community:update'" type="setting" theme="twoTone" twoToneColor="#4a9ff5"
            @click="edit(record)" title="修改小区"></a-icon>
          &nbsp;
          <a-icon type="eye" theme="twoTone" twoToneColor="#42b983" @click="view(record)" title="查看"></a-icon>
        </template>
      </a-table>
      <!-- 信息查看 -->
      <community-info @close="handleCommunityInfoClose" :communityInfoVisiable="communityInfo.visiable"
        :communityInfoData="communityInfo.data">
      </community-info>
      <!-- 新增小区 -->
      <community-add @close="handleCommunityAddClose" @success="handleCommunityAddSuccess"
        :communityAddVisiable="communityAdd.visiable">
      </community-add>
      <!-- 修改小区 -->
      <community-edit ref="communityEdit" :communityInfoData="communityInfo.data" @close="handleCommunityEditClose"
        @success="handleCommunityEditSuccess" :communityEditVisiable="communityEdit.visiable">
      </community-edit>
    </div>
  </a-card>
</template>

<script>
  import RangeDate from '@/components/datetime/RangeDate'
  import CommunityAdd from './CommunityAdd'
  import CommunityInfo from './CommunityInfo'
  import CommunityEdit from './CommunityEdit'

  export default {
    name: 'Community',
    components: {
      RangeDate,
      CommunityInfo,
      CommunityAdd,
      CommunityEdit
    },
    data() {
      return {
        advanced: false,
        communityInfo: {
          visiable: false,
          data: {}
        },
        communityAdd: {
          visiable: false
        },
        communityEdit: {
          visiable: false
        },
        queryParams: {
          createTimeFrom: '',
          createTimeTo: ''
        },
        dataSource: [],
        sortedInfo: null,
        paginationInfo: null,
        selectedRowKeys: [],
        pagination: {
          pageSizeOptions: ['10', '20', '30', '40', '100'],
          defaultCurrent: 1,
          defaultPageSize: 10,
          showQuickJumper: true,
          showSizeChanger: true,
          showTotal: (total, range) => `显示 ${range[0]} ~ ${range[1]} 条记录，共 ${total} 条记录`
        },
        loading: false
      }
    },
    computed: {
      columns() {
        let {
          sortedInfo
        } = this
        sortedInfo = sortedInfo || {}
        return [{
          title: '名称',
          dataIndex: 'commName'
        }, {
          title: '地址',
          dataIndex: 'address'
        }, {
          title: '描述',
          dataIndex: 'remark',
          scopedSlots: {
            customRender: 'remark'
          },
          width: 350
        }, {
          title: '创建时间',
          dataIndex: 'createTime',
          sorter: true,
          sortOrder: sortedInfo.columnKey === 'createTime' && sortedInfo.order
        }, {
          title: '操作',
          dataIndex: 'operation',
          scopedSlots: {
            customRender: 'operation'
          }
        }]
      }
    },
    mounted() {
      this.fetch()
    },
    methods: {
      onSelectChange(selectedRowKeys) {
        this.selectedRowKeys = selectedRowKeys
      },
      add() {
        this.communityAdd.visiable = true
      },
      handleCommunityAddClose() {
        this.communityAdd.visiable = false
      },
      handleCommunityAddSuccess() {
        this.communityAdd.visiable = false
        this.$message.success('新增小区成功')
        this.search()
      },
      view(record) {
        this.communityInfo.data = record
        this.communityInfo.visiable = true
      },
      handleCommunityInfoClose() {
        this.communityInfo.visiable = false
      },
      edit(record) {
        this.$refs.communityEdit.setFormValues(record)
        this.communityInfo.data = record
        this.communityEdit.visiable = true
      },
      handleCommunityEditSuccess() {
        this.communityEdit.visiable = false
        this.$message.success('修改小区成功')
        this.search()
      },
      handleCommunityEditClose() {
        this.communityEdit.visiable = false
      },
      handleDateChange(value) {
        if (value) {
          this.queryParams.createTimeFrom = value[0]
          this.queryParams.createTimeTo = value[1]
        }
      },
      batchDelete() {
        if (!this.selectedRowKeys.length) {
          this.$message.warning('请选择需要删除的记录')
          return
        }
        let that = this
        this.$confirm({
          title: '确定删除所选中的记录?',
          content: '当您点击确定按钮后，这些记录将会被彻底删除',
          centered: true,
          onOk() {
            let communityIds = []
            for (let key of that.selectedRowKeys) {
              communityIds.push(that.dataSource[key].commId)
            }
            that.$delete('community/' + commIds.join(',')).then(() => {
              that.$message.success('删除成功')
              that.selectedRowKeys = []
              that.search()
            })
          },
          onCancel() {
            that.selectedRowKeys = []
          }
        })
      },
      exprotExccel() {
        let {
          sortedInfo
        } = this
        let sortField, sortOrder
        // 获取当前列的排序和列的过滤规则
        if (sortedInfo) {
          sortField = sortedInfo.field
          sortOrder = sortedInfo.order
        }
        this.$export('community/excel', {
          sortField: sortField,
          sortOrder: sortOrder,
          ...this.queryParams
        })
      },
      search() {
        let {
          sortedInfo
        } = this
        let sortField, sortOrder
        // 获取当前列的排序和列的过滤规则
        if (sortedInfo) {
          sortField = sortedInfo.field
          sortOrder = sortedInfo.order
        }
        this.fetch({
          sortField: sortField,
          sortOrder: sortOrder,
          ...this.queryParams
        })
      },
      reset() {
        // 取消选中
        this.selectedRowKeys = []
        // 重置分页
        this.$refs.TableInfo.pagination.current = this.pagination.defaultCurrent
        if (this.paginationInfo) {
          this.paginationInfo.current = this.pagination.defaultCurrent
          this.paginationInfo.pageSize = this.pagination.defaultPageSize
        }
        // 重置列排序规则
        this.sortedInfo = null
        // 重置查询参数
        this.queryParams = {}
        // 清空时间选择
        this.$refs.createTime.reset()
        this.fetch()
      },
      handleTableChange(pagination, filters, sorter) {
        // 将这两个参数赋值给Vue data，用于后续使用
        this.paginationInfo = pagination
        this.sortedInfo = sorter
        this.fetch({
          sortField: sorter.field,
          sortOrder: sorter.order,
          ...this.queryParams
        })
      },
      fetch(params = {}) {
        this.loading = true
        if (this.paginationInfo) {
          // 如果分页信息不为空，则设置表格当前第几页，每页条数，并设置查询分页参数
          this.$refs.TableInfo.pagination.current = this.paginationInfo.current
          this.$refs.TableInfo.pagination.pageSize = this.paginationInfo.pageSize
          params.pageSize = this.paginationInfo.pageSize
          params.pageNum = this.paginationInfo.current
        } else {
          // 如果分页信息为空，则设置为默认值
          params.pageSize = this.pagination.defaultPageSize
          params.pageNum = this.pagination.defaultCurrent
        }
        this.$get('community', {
          ...params
        }).then((r) => {
          let data = r.data
          const pagination = {
            ...this.pagination
          }
          pagination.total = data.total
          this.dataSource = data.rows
          this.pagination = pagination
          this.loading = false
        })
      }
    }
  }

</script>

<style lang="less" scoped>
  @import "../../../../static/less/Common";

</style>
